package com.treehouse.pornstar.services.impl;

import java.sql.Connection;
import java.util.LinkedList;

import com.treehouse.logging.Logger;
import com.treehouse.pornstar.Pornstar;
import com.treehouse.pornstar.dao.IPornstarDao;
import com.treehouse.pornstar.dao.impl.PornstarDaoImpl;
import com.treehouse.pornstar.services.IPornstarServices;
import com.treehouse.service.application.IApplicationService;
import com.treehouse.url.HTMLParser;
import com.treehouse.url.farmer.IURLFarmer;
import com.treehouse.url.farmer.URLFarmer;

public class PornstarServicesImpl implements IPornstarServices {

	private IPornstarDao pornstarDao;
	private HTMLParser parser;
	private IURLFarmer farmer;
	private IApplicationService applicationService;

	
	public PornstarServicesImpl(IApplicationService applicationService, Connection connection){
		this.applicationService = applicationService;
		this.pornstarDao = new PornstarDaoImpl(connection);
		this.parser = new HTMLParser();
		this.farmer = new URLFarmer(
				applicationService.getPornstarEntry(),
				applicationService.getDomain(), parser);
		}
	
	
	
	
	
	@Override
	public boolean hasToReload() {
		return false;
	}
	

	@Override
	public Pornstar getPornstar(int id) {
		return this.pornstarDao.selectPornstar(id);
	}

	@Override
	public Pornstar getPornstar(String name) {
		return this.pornstarDao.selectPornstar(name);
	}

	@Override
	public Pornstar[] getPornstars() {
		return this.pornstarDao.selectPornstars();
	}

	@Override
	public boolean setPornstar(Pornstar pornstar) {
		if (this.pornstarDao.insertPornstar(pornstar))
			return true;
		return false;
	}

	@Override
	public boolean pornstarExists(int id) {
		if (this.pornstarDao.selectPornstar(id) != null)
			return true;
		return false;
	}

	@Override
	public boolean pornstarExists(String name) {
		if (this.pornstarDao.selectPornstar(name) != null)
			return true;
		return false;
	}

	@Override
	public Pornstar[] getPornstarsForScene(int id) {
		return this.pornstarDao.selectPornstarsForScene(id);
	}

	@Override
	public Pornstar[] getPornstarsForScene(String name) {
		return this.pornstarDao.selectPornstarsForScene(name);
	}

	@Override
	public LinkedList<Pornstar> reloadPornstarListToDB() {
		LinkedList<Pornstar> list = this.farmer.getPornstarList();
		Logger.inf("Adding Actors to Database..");
		for(Pornstar actor : list){
			this.pornstarDao.insertPornstar(actor);
		}
		return list;
	}

}
